package com.wl.index.Controller;

import com.wl.entity.Note;
import com.wl.bean.Result;
import com.wl.index.Biz.ICommentsBiz;
import com.wl.index.Biz.INoteBiz;
import com.wl.index.Biz.Impl.FileStorageBizImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/index")
@CrossOrigin(origins = "*")
public class NoteController {
    @Autowired
    private INoteBiz noteBiz;

    @Autowired
    private ICommentsBiz commentsBiz;

    @Autowired
    private FileStorageBizImpl fileStorageBiz;

    @RequestMapping("/noteinfo")
    public Result getNoteUserInfo(Integer nid,String title) {
        if (nid != null){
            Note note = noteBiz.selectNoteauthorById(nid);
            if (note != null){
                // 转换图片路径为完整URL
                note.setImg1("/lv-index/files/" + note.getImg1());
                note.setImg2("/lv-index/files/" + note.getImg2());
                note.setImg3("/lv-index/files/" + note.getImg3());
                System.out.println("查询到的帖子信息：" + note);
                return new Result(1,"查询成功",note);//返回note对象
            }
            return new Result(0,"该贴不存在",null);
        }
        if (title != null && !title.isEmpty()){
            List<Note> notes = noteBiz.selectByTitle(title);
            if (!notes.isEmpty()){
                return new Result(1,"查询成功",notes);//返回notes列表
            }
        }
        return new Result(0,"请提供查询参数",null);
    }

    //查询所有帖子
    @GetMapping("/notes")
    public Result getAllValidNotes(Integer uid) {
        try {
            // 调用服务层（暂未实现分页）
            List<Note> notes = noteBiz.selectAllValidNotes(uid);

            // 转换所有笔记的图片路径
            notes.forEach(note -> {
                note.setImg1("/lv-index/files/" + note.getImg1());
                note.setImg2("/lv-index/files/" + note.getImg2());
                note.setImg3("/lv-index/files/" + note.getImg3());
            });

            // 构造返回结果
            Map<String, Object> resultData = new HashMap<>();
            resultData.put("list", notes);
            resultData.put("total", notes.size());
            return new Result(1, "获取成功", resultData);

        }catch (Exception e) {
            return new Result(0, "系统繁忙，请稍后再试", null);
        }
    }

    //生成帖子
    @PostMapping(value = "/note", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public Result createNote(@RequestParam("uid") Integer uid,
                             @RequestParam("title") String title,
                             @RequestParam("content") String content,
                             @RequestParam(value = "img1", required = false) MultipartFile img1,
                             @RequestParam(value = "img2", required = false) MultipartFile img2,
                             @RequestParam(value = "img3", required = false) MultipartFile img3) {
        try {
            // 1. 创建Note对象
            Note note = new Note();
            note.setUid(uid);
            note.setTitle(title);
            note.setContent(content);

            if (note.getUid() == null) {
                return new Result(0, "用户未登录", null);
            }

            // 2. 处理文件上传
            if (img1 != null && !img1.isEmpty()) {
                // 保存img1并设置路径到note对象
                String img1Path = fileStorageBiz.storeFile(img1);
                note.setImg1(img1Path);
            }
            if (img2 != null && !img2.isEmpty()) {
                // 保存img1并设置路径到note对象
                String img2Path = fileStorageBiz.storeFile(img2);
                note.setImg2(img2Path);
            }
            if (img3 != null && !img3.isEmpty()) {
                // 保存img1并设置路径到note对象
                String img3Path = fileStorageBiz.storeFile(img3);
                note.setImg3(img3Path);
            }

            int result = noteBiz.insertNote(note);

            // 根据操作结果构造响应
            if (result > 0) {
                Map<String, Object> data = new HashMap<>();
                data.put("nid", note.getNid()); // 返回生成的主键
                return new Result(1, "帖子创建成功", data);
            }
            return new Result(0, "帖子创建失败", null);
        } catch (IllegalArgumentException e) {
            // 捕获业务校验异常
            return new Result(0, e.getMessage(), null);
        } catch (Exception e) {
            // 系统级异常处理
            e.printStackTrace();
            return new Result(0, "系统繁忙，请稍后重试: " + e.getMessage(), null);
        }
    }
}
